package com.vcc.web.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.vcc.core.annotations.CurrentROStatus;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.ProgramException;
import com.vcc.core.model.file.tran.AuthTranFilPojo;
import com.vcc.core.util.DateUtil;
import com.vcc.core.util.FileUtil;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.Role;
import com.vcc.data.entity.Transaction;
import com.vcc.data.entity.User;
import com.vcc.service.client.vo.trans.TransactionVo;
import com.vcc.service.service.ClearTranService;
import com.vcc.service.service.TransactionService;
import com.vcc.service.service.UserCardService;
import com.vcc.service.service.UserService;
import com.vcc.web.core.util.UserUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("trans")
public class TransController {

    @Autowired
    private UserCardService userCardService;
    @Autowired
    private UserService userService;
    @Autowired
    private TransactionService transactionService;
    @Autowired
    private ClearTranService clearTranService;


    @RestPack
    @GetMapping("authPaging")
    @SaCheckPermission("card:transaction:view")
    public Page<TransactionVo> authPaging(
            Page<Transaction> pageable,
            Long targetUserId,
            String cardNumber,
            String startDate,
            String endDate,
            String type,
            String status,
            Long userCardId
    ) {
        User user = UserUtil.get();
        Long parentId = user.getParentId();
        Role role = UserUtil.getRole();
        Page<Transaction> paging = transactionService.paging(
                role.getHasAllData() && parentId != null && parentId != 0 ? parentId : user.getId(),
                role, pageable, targetUserId, status, type, cardNumber, startDate, endDate, userCardId);
        return paging.map(it -> {
            String account = userService.getAccountById(it.getUserId());
            return new TransactionVo(it, account);
        });

    }


    @GetMapping("deriveAuthData")
    @SaCheckPermission("card:transaction:derive")
    public void deriveAuthData(
            @CurrentROStatus Boolean roStatus,
            Long targetUserId,
            String cardNumber,
            String startDate,
            String endDate,
            String type,
            String status,
            Long userCardId,
            HttpServletResponse response
    ) {
        if (RegexUtil.checkStringIsNull(startDate)) {
            throw new ProgramException("请选择开始时间");
        }
        if (RegexUtil.checkStringIsNull(endDate)) {
            throw new ProgramException("请选择结束时间");
        }
        User user = UserUtil.get();
        Long parentId = user.getParentId();
        Role role = UserUtil.getRole();
        Long pages = 1L;
        Page<Transaction> pageable;
        Page<Transaction> paging;
        SXSSFWorkbook sxssfWorkbook = FileUtil.createTranTitle(10000);
        do {
            pageable = new Page<>(pages++, 20);
            paging = transactionService.paging(
                    role.getHasAllData() && parentId != null && parentId != 0 ? parentId : user.getId(),
                    role, pageable, targetUserId, status, type, cardNumber, startDate, endDate,userCardId);
            List<AuthTranFilPojo> list = paging.getRecords().stream().map(it -> {
                String account = userService.getAccountById(it.getUserId());
                return new AuthTranFilPojo(
                        it.getTranId(),
                        it.getOriginalTranId(),
                        account,
                        it.getCardNumber(),
                        it.getCardTypeId(),
                        it.getMerchantName(),
                        DateUtil.format(it.getTranDate()),
                        it.getSettleDate() == null ? "" : DateUtil.format(it.getSettleDate()),
                        it.getBillingAmount(),
                        it.getBillingCurrency(),
                        it.getMerchantAmount(),
                        it.getMerchantCurrency(),
                        it.getSettleAmount(),
                        it.getSettleCurrency(),
                        it.getTransactionType(),
                        it.getRemark(),
                        it.getStatus(),
                        it.getMcc());
            }).collect(Collectors.toList());
            FileUtil.deriveTranData(sxssfWorkbook, list, paging.getPageNumber(), paging.getPageSize());
        } while (!(pages).equals(paging.getTotalPage() + 1));
        String fileName = System.currentTimeMillis() + "授权交易明细.xlsx";

        FileUtil.downXlsxFile(response, sxssfWorkbook, fileName);

    }

//
//    @RestPack
//    @GetMapping("clearPaging")
//    @SaCheckPermission("clear:view")
//    public Page<ClearTranVo> clearPaging(
//            Page<ClearTran> pageable,
//            Long targetUserId,
//            String cardNumber,
//            String startDate,
//            String endDate,
//            String type
//    ) {
//        User user = UserUtil.get();
//        Long parentId = user.getParentId();
//        Role role = UserUtil.getRole();
//        Page<ClearTran> paging = clearTranService.paging( role.getHasAllData() && parentId != 0 ? parentId : user.getId(), role, pageable, targetUserId, type, cardNumber, startDate, endDate);
//        return paging.map(it -> {
//            String account = userService.getAccountById(it.getUserId());
//            return new ClearTranVo(it, account);
//        });
//    }
//
//
//    @GetMapping("deriveClearData")
//    @SaCheckPermission("clear:derive")
//    public void deriveClearData(
//            @CurrentROStatus Boolean roStatus,
//            Long targetUserId,
//            String cardNumber,
//            String startDate,
//            String endDate,
//            String type,
//            HttpServletResponse response
//    ) {
//        User user = UserUtil.get();
//        Long parentId = user.getParentId();
//        Role role = UserUtil.getRole();
//        Long pages = 1L;
//        Page<ClearTran> pageable;
//        Page<ClearTran> paging;
//        SXSSFWorkbook sxssfWorkbook = FileUtil.createClearTitle(10000);
//        do {
//            pageable = new Page<>(pages++, 20);
//            paging = clearTranService.paging( role.getHasAllData() && parentId != 0 ? parentId : user.getId(), role, pageable, targetUserId, type, cardNumber, startDate, endDate);
//            List<ClearTranFilPojo> list = paging.getRecords().stream().map(it -> {
//                String account = userService.getAccountById(it.getUserId());
//                return new ClearTranFilPojo(
//                        it.getTransactionId(),
//                        account,
//                        it.getCardNumber(),
//                        it.getCardId(),
//                        it.getMerchantName(),
//                        DateUtil.format("yyyy-MM-dd", it.getTranDate()),
//                        DateUtil.format("yyyy-MM-dd", it.getPostingDate()),
//                        it.getBillingAmount(),
//                        it.getBillingCurrency(),
//                        it.getMerchantAmount(),
//                        it.getMerchantCurrency(),
//                        it.getType(),
//                        it.getRemark()
//                );
//            }).collect(Collectors.toList());
//            FileUtil.deriveClearData(sxssfWorkbook, list, paging.getPageNumber(), paging.getPageSize());
//        } while (!(pages).equals(paging.getTotalPage() + 1));
//        String fileName = System.currentTimeMillis() + "清算交易明细.xlsx";
//
//            FileUtil.downXlsxFile(response, sxssfWorkbook, fileName);
//
//    }
}
